export default {
	config:{
		baseUrl:"",
		header:{
			"Content-Type":"application/x-www-form-urlencoded"
		},
		dataType:"json",
		responseType:"text",
		env:'dev'
	},
	
	interceptor: {
		request: null,
		response: null
	},
	request(options) {

		return new Promise((resolve, reject) => {
			let _config = null
			options.url = this.config.baseUrl + options.url
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config
				if (this.config.env === 'dev') {
					if(statusCode === 200) {
						// console.log("返回结果：" + JSON.stringify(response.data))
					}else{
						wx.hideLoading();
						wx.showToast({title:'异常错误，请刷新重试',icon:'none',mask:true})
					}
				}
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				console.log(statusCode)
				if(statusCode === 200) { //成功
					resolve(response);
				}else{
					wx.hideLoading();
					wx.showToast({title:'异常错误，请刷新重试',icon:'none',mask:true})
					reject(response)
				}
			}
			options.fail = (err) => {
				console.log(err)
				wx.hideLoading();
				wx.showToast({title:err.message,icon:'none',mask:true})
				reject(err)
			}
			_config = Object.assign({},this.config,options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}
			if (this.config.env === 'dev') {
				// console.log("请求地址：" + _config.url)
				if (_config.data) {
					// console.log("请求参数：" + JSON.stringify(_config.data))
				}
			}
			uni.request(_config);
		});
		
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		console.log(123)
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}